﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RuiJing.Web.Commen;
using Code.BLL;
using Code.Models;
using RuiJing.Web.Areas.Manage.Models.LoginModels;
using Code.Commen;
using System.Web.Security;

namespace RuiJing.Web.Areas.Manage.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Manage/Login/

        public ActionResult Index()
        {
            if (User.IsInRole("Manage"))
                return RedirectToAction("index", "home");
            var model = new IndexModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(IndexModel model)
        {
            if (!model.CheckCode.Equals(TempData["CheckCode"] as string, StringComparison.CurrentCultureIgnoreCase))
                ModelState.AddModelError("CheckCode", "验证码不正确");
            if (ModelState.IsValid)
            {
                var MM = new ManagerManage();
                try
                {
                    var am = MM.CheckPwd(model.LoginName, model.Password);

                    var aus = System.Configuration.ConfigurationManager.GetSection("system.web/authentication") as System.Web.Configuration.AuthenticationSection;
                    if (aus != null)
                    {

                        FormsAuthenticationTicket tick = new FormsAuthenticationTicket(2, am.ID.ToString("N"), DateTime.Now, DateTime.Now.AddMinutes(aus.Forms.Timeout.TotalMinutes), false, "Manage");
                        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(tick));
                        Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                        Response.Cookies.Add(cookie);
                    }
                    else
                        throw new AppException("设置错误");


                    return Redirect(Request["return"] ?? Url.Action("index", "home"));
                }
                catch (ValidateException e)
                {
                    ModelState.AddModelError("Error", e.Message);
                }
            }

            return View(model);
        }

        public ActionResult Out()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("index");
        }

        public FileContentResult CheckCode(string id)
        {
            var codebuild = new CheckCodeBuilder();
            string code = codebuild.CreateVerifyCode();
            var img = codebuild.CreateImageCode(code);
            var ms = new System.IO.MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            var bytes = ms.ToArray();
            ms.Dispose();
            img.Dispose();
            if (string.IsNullOrEmpty(id))
                id = "CheckCode";
            TempData[id] = code;
            return new FileContentResult(bytes, "image/Jpeg");
        }

    }
}
